#include "cgal_defines.hpp"
#include "cgal_source_edge.h"
#include "cgal_source_halfedge.h"
#include "cgal_source_point.h"
#include "cgal_source_poly.h"

#include <CGAL/IO/STL.h>
#include <iostream>
#include <memory>

#include <CGAL/polygon_mesh_processing.h>

#include <vtkActor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

int main() {
  auto mesh = std::make_shared<surfacemesh_type>();
  if (!CGAL::IO::read_STL("test.stl", *mesh) || mesh->is_empty()) {
    std::cerr << "Failed to read STL or mesh is empty.\n";
    return -1;
  }

  vtkNew<cgalView::cgal_source_halfedge> source;
  source->set_arrow_length(0.1);
  source->set_halfedge_offset(0.01);
  source->load_file("test.stl");
  source->Update(); // 确保调用

  vtkNew<cgalView::cgal_source_poly> esource;
  esource->load_mesh(source->get_mesh());
  esource->Update();

  vtkNew<vtkNamedColors> color;

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> render_window;
  render_window->AddRenderer(renderer);
  render_window->SetSize(800, 640);
  render_window->SetWindowName("TestSurfaceMeshSource");

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(source->GetOutputPort());

  vtkNew<vtkPolyDataMapper> emapper;
  emapper->SetInputConnection(esource->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(1.0, 0.0, 0.0); // 实体颜色
  actor->GetProperty()->SetRepresentationToSurface();

  vtkNew<vtkActor> eactor;
  eactor->SetMapper(emapper);
  eactor->GetProperty()->SetColor(1.0, 1.0, 0.0);

  renderer->AddActor(actor);
  renderer->AddActor(eactor);
  renderer->SetBackground(color->GetColor3d("Burlywood").GetData());

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(render_window);

  vtkNew<vtkInteractorStyleTrackballCamera> style{};
  interactor->SetInteractorStyle(style);

  interactor->Initialize();
  renderer->ResetCamera();
  render_window->Render();
  interactor->Start();
}